package com.lrd.snapshotorder.controller;

import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
public class TestController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    //-------------------multipartFile
    @PostMapping("uploadFiles")
    public String uploadPictureList(
            @RequestParam(value = "file", required = false) MultipartFile[] files,
            HttpServletRequest request) throws FileNotFoundException {

        for (MultipartFile ff : files) {
            System.out.println("file转multipartFile成功. " + ff.getOriginalFilename());
        }

        List<MultipartFile> fileList = ((StandardMultipartHttpServletRequest) request).getFiles("multipartFiles");


        return null;
    }

    //-------------------------redis
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/testRedisson")
    public String testRedisson() throws InterruptedException {
        RBucket<Object> test001 = redissonClient.getBucket("test001");
        test001.set("hello", 10, TimeUnit.SECONDS);
        Thread.sleep(8000);
        Object o = test001.get();
        System.out.println(o);
        Thread.sleep(3000);
        System.out.println(test001.get());
        return "success";
    }

    @GetMapping("/testCountDownLatch")
    public String testCountDownLatch() {
        int count = 50;
        CountDownLatch latch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        String lockKey = "17631701110";
                        RLock lock = redissonClient.getLock(lockKey);
                        lock.lock(2000, TimeUnit.MILLISECONDS);
                        System.out.println("===加锁===" + Thread.currentThread().getName());
                        System.out.println("===做自己操作===");
                        //Thread.sleep(1000);
                        System.out.println("===释放锁===" + Thread.currentThread().getName());
                        lock.unlock();
                        System.out.println(latch.getCount());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    latch.countDown();
                }
            };
            thread.start();
        }
        return "success";
    }

}
